package com.zy.xuanwo.utils;


import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * String工具类
 * @author Administrator
 *
 */
public class StringUtil {
	/**
	 * 空字符
	 */
	public static final String EMPTY = "";
	
	
	 /**
     * <p>Checks if a CharSequence is empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     *
     * <p>NOTE: This method changed in Lang version 2.0.
     * It no longer trims the CharSequence.
     * That functionality is available in isBlank().</p>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is empty or null
     * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * <p>Checks if a CharSequence is not empty ("") and not null.</p>
     *
     * <pre>
     * StringUtils.isNotEmpty(null)      = false
     * StringUtils.isNotEmpty("")        = false
     * StringUtils.isNotEmpty(" ")       = true
     * StringUtils.isNotEmpty("bob")     = true
     * StringUtils.isNotEmpty("  bob  ") = true
     * </pre>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is not empty and not null
     * @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
     */
    public static boolean isNotEmpty(CharSequence cs) {
        return !StringUtil.isEmpty(cs);
    }

    /**
     * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is null, empty or whitespace
     * @since 2.0
     * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
     */
    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
     *
     * <pre>
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * </pre>
     *
     * @param cs  the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is
     *  not empty and not null and not whitespace
     * @since 2.0
     * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
     */
    public static boolean isNotBlank(CharSequence cs) {
        return !StringUtil.isBlank(cs);
    }

    // Trim
    //-----------------------------------------------------------------------
    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * ends of this String, handling {@code null} by returning
     * {@code null}.</p>
     *
     * <p>The String is trimmed using {@link String#trim()}.
     * Trim removes start and end characters &lt;= 32.
     * To strip whitespace use {@link #strip(String)}.</p>
     *
     * <p>To trim your choice of characters, use the
     * {@link #strip(String, String)} methods.</p>
     *
     * <pre>
     * StringUtils.trim(null)          = null
     * StringUtils.trim("")            = ""
     * StringUtils.trim("     ")       = ""
     * StringUtils.trim("abc")         = "abc"
     * StringUtils.trim("    abc    ") = "abc"
     * </pre>
     *
     * @param str  the String to be trimmed, may be null
     * @return the trimmed string, {@code null} if null String input
     */
    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * ends of this String returning {@code null} if the String is
     * empty ("") after the trim or if it is {@code null}.
     *
     * <p>The String is trimmed using {@link String#trim()}.
     * Trim removes start and end characters &lt;= 32.
     * To strip whitespace use {@link #stripToNull(String)}.</p>
     *
     * <pre>
     * StringUtils.trimToNull(null)          = null
     * StringUtils.trimToNull("")            = null
     * StringUtils.trimToNull("     ")       = null
     * StringUtils.trimToNull("abc")         = "abc"
     * StringUtils.trimToNull("    abc    ") = "abc"
     * </pre>
     *
     * @param str  the String to be trimmed, may be null
     * @return the trimmed String,
     *  {@code null} if only chars &lt;= 32, empty or null String input
     * @since 2.0
     */
    public static String trimToNull(String str) {
        String ts = trim(str);
        return isEmpty(ts) ? null : ts;
    }

    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * ends of this String returning an empty String ("") if the String
     * is empty ("") after the trim or if it is {@code null}.
     *
     * <p>The String is trimmed using {@link String#trim()}.
     * Trim removes start and end characters &lt;= 32.
     * To strip whitespace use {@link #stripToEmpty(String)}.</p>
     *
     * <pre>
     * StringUtils.trimToEmpty(null)          = ""
     * StringUtils.trimToEmpty("")            = ""
     * StringUtils.trimToEmpty("     ")       = ""
     * StringUtils.trimToEmpty("abc")         = "abc"
     * StringUtils.trimToEmpty("    abc    ") = "abc"
     * </pre>
     *
     * @param str  the String to be trimmed, may be null
     * @return the trimmed String, or an empty String if {@code null} input
     * @since 2.0
     */
    public static String trimToEmpty(String str) {
        return str == null ? EMPTY : str.trim();
    }
	
	/**
	 * 格式化数字，格式化数字为两位数的数字，不足填0，多余的保留前二位
	 * @param source 数字
	 * @return 格式化的数字
	 */
	public static String formatToTwoDigitally(Integer source){
		if(source == null){
			return "00";
		}
		String result = source.toString();
		//String result = String.format("%02d", source);
		if(result.length() > 2){
			result = result.substring(0, 2);
		}else if(result.length() == 1){
			result = "0"+result;
		}
		return result;
	}
	/**
	 * 返回一个特定编码格式的字符
	 * @param ch 字符内容
	 * @param start 初始偏移量
	 * @param length 长度
	 * @param character 字符编码格式
	 * @return
	 */
	public static String formatStringToCharset(char[] ch, int start, int length, String character){
		if(ch.length == 0){
			return null;
		}
		try {
			return new String(new String(ch,start,length).getBytes(),character);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 返回一个字符的字符
	 * @param ch 字符内容
	 * @param start 初始偏移量
	 * @param length 长度
	 * @return
	 */
	public static String formatStringToCharset(byte[] data, int start, int length){
		if(data.length == 0){
			return null;
		}
		return new String(data, start, length, Charset.defaultCharset());
	}
	/**
	 * 返回一个新的字节数组
	 * <ul>
	 * 	<li>如果参数中的字节数组长度小于length的话返回新的字节数组则是在data字节数组和不足补{@link TagUtil}类的SUBSTITUTIONBYTE常量的字符数组</li>
	 * 	<li>如果参数中的字节数组长度大于length的话返回新的字节数组则是data字节数中0到lenght-1的字符数组</li>
	 * 	<li>如果参数中的字节数组长度等于length的话返回新的字节数组则是复制data字节数字符数组</li>
	 * </ul>
	 * @param data 字节数组
	 * @param length 返回字节数组的长度
	 * @return
	 */
	public static byte[] subByte(byte[] data,int length){
		byte[] result = new byte[length];
		if(data == null){
			int i = 0;
			while(i < length){
//				result[i] = TagUtil.SUBSTITUTIONBYTE;
				i++;
			}
		}else{
			if(data.length < length){
				System.arraycopy(data, 0, result, 0, data.length);
				int i = data.length;
				while(i < length){
//					result[i] = TagUtil.SUBSTITUTIONBYTE;
					i++;
				}
			}else{
				System.arraycopy(data, 0, result, 0, length);
			}
		}
		return result;
	}
	
	/**
	 * 返回一个新的字节数组
	 * <ul>
	 * 	<li>如果参数中的字节数组长度小于length的话返回新的字节数组则是在data字节数组和不足补{@link substitution}的字符数组</li>
	 * 	<li>如果参数中的字节数组长度大于length的话返回新的字节数组则是data字节数中0到lenght-1的字符数组</li>
	 * 	<li>如果参数中的字节数组长度等于length的话返回新的字节数组则是复制data字节数字符数组</li>
	 * </ul>
	 * @param data 字节数组
	 * @param substitution 补位字节
	 * @param length 返回字节数组的长度
	 * @return
	 */
	public static byte[] subByte(byte[] data,byte substitution,int length){
		byte[] result = new byte[length];
		if(data == null || data.length == 0){
			int i = 0;
			while(i < length){
				result[i] = substitution;
				i++;
			}
		}else{
			if(data.length < length){
				System.arraycopy(data, 0, result, 0, data.length);
				int i = data.length;
				while(i < length){
					result[i] = substitution;
					i++;
				}
			}else{
				System.arraycopy(data, 0, result, 0, length);
			}
		}
		return result;
	}
	
	/**
	 * 返回一个字节数组
	 * <ul>
	 * 	<li>如果参数中的字符长度小于length的话返回字节数组则是字符的字节数组和不足补空格的组成字符数组</li>
	 * 	<li>如果参数中的字符长度大于length的话返回字节数组则是字符的字节数组中0到lenght-1的组成字符数组</li>
	 * 	<li>如果参数中的字符长度等于length的话返回字节数组则是复制字符的字节数组</li>
	 * </ul>
	 * @param data 字符
	 * @param length 返回字节数组的长度
	 * @return
	 */
	public static byte[] subByte(String string, int length){
		byte[] data = null;
		if(isNotEmpty(string)){
			data = string.getBytes();
		}
		return subByte(data, length);
	}
	
	/**
	 * 把内容复制多份，返回复制的内容
	 * @param d 内容
	 * @param number 份数
	 * @return
	 */
	public static byte[] copy(byte d,int number){
		byte[] result = new byte[number];
		for (int i = 0; i < number; i++) {
			result[i] = d;
		}
		return result;
	}
	
}
